﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using Microsoft.Office.Interop.Excel;
using TpLogger;
using myOffice = Microsoft.Office.Interop.Word;

namespace TpTools {
    public class Tools {

        #region Members

        public enum tpCopyType {
            JobEvidenz = 1,
            Leistungen = 2,
            Kalkulation = 3
        }
        public enum tpContent {
            AllJobs = 1,
            ProjectJobs = 2,
            InternJobs = 3
        }
        public enum tpSorting {
            Date = 1,
            Area = 2,
            Hour = 3
        }
        public enum tpSortOrder {
            Up = 1,
            Down = 2
        }
        public enum tpShow {
            Area = 1,
            List = 3
        }

        private static List<DateTime> Feiertage = new List<DateTime> { new DateTime(2012, 1, 1), 
		  new DateTime(2012, 1, 6), 
		  new DateTime(2012, 4, 9), 
		  new DateTime(2012, 5, 1),
		  new DateTime(2012,5,17),
		  new DateTime(2012,5,28), 
		  new DateTime(2012,6,7), 
		  new DateTime(2012,8,15), 
		  new DateTime(2012,10,26), 
		  new DateTime(2012,11,1), 
		  new DateTime(2012,12,8), 
		  new DateTime(2012,12,25), 
		  new DateTime(2012,12,26), 
		  new DateTime(2013,1,1), 
		  new DateTime(2013,1,6), 
		  new DateTime(2013,4,1), 
		  new DateTime(2013,5,1), 
		  new DateTime(2013,5,9), 
		  new DateTime(2013,5,20), 
		  new DateTime(2013,5,30), 
		  new DateTime(2013,8,15), 
		  new DateTime(2013,10,26), 
		  new DateTime(2013,11,1), 
		  new DateTime(2013,12,8), 
		  new DateTime(2013,12,25), 
		  new DateTime(2013,12,26)
	  };

        #endregion

        #region Constructor
        #endregion

        #region Properties
        #endregion

        #region Methods

        public static void SendMail(string from, string to, string subject, string msg, object att, Logger log) {

            try {
                SmtpClient mail = new SmtpClient("tp-s01");
                mail.DeliveryMethod = SmtpDeliveryMethod.Network;
                //mail.Credentials = new NetworkCredential("Es", "Epson1@tp");
                mail.UseDefaultCredentials = true;
                mail.EnableSsl = false;
                mail.Send(from, to, subject, msg);



            } catch(Exception ex) {
                log.Add("SendMail", ex);
            }
        }

        public static string SaveAsPDF(myOffice.Document mDoc, Logger log) {
            object fileFormat = myOffice.WdSaveFormat.wdFormatPDF;
            object oMissing = System.Reflection.Missing.Value;
            string fileName = "";
            try {
                fileName = mDoc.FullName.Replace(".docx", ".pdf");
                if(File.Exists(fileName)) {
                    File.Delete(fileName);
                }
                mDoc.SaveAs2(fileName, fileFormat);
                return fileName;
            } catch(Exception ex) {
                fileName = "";
                log.Add("SaveAsPDF", ex);
            }
            return fileName;
        }

        #endregion

        #region Functions

        public static bool ExistsContentInColumn(Worksheet sh, string column, object value) {
            Range col1 = sh.Columns[column];
            Range erg = col1.Find(value);
            return erg != null;
        }

        public static bool FileIsInUse(string fileName) {
            try {
                using(Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Write)) {
                }
                return false;
            } catch {
                return true;
            }
        }

        public static DateTime GetFirstDayOfMonth(DateTime dateTime) {
            return new DateTime(dateTime.Year, dateTime.Month, 1);
        }

        public static DateTime GetFirstDayOfWeek(DateTime dateTime) {
            while(dateTime.DayOfWeek != DayOfWeek.Monday)
                dateTime = dateTime.Subtract(new TimeSpan(1, 0, 0, 0));
            return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);
        }

        public static DateTime GetLastDayOfMonth(DateTime dateTime) {
            DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
            return firstDayOfTheMonth.AddMonths(1).AddDays(-1);
        }

        public static DateTime GetLastDayOfWeek(DateTime dateTime) {
            while(dateTime.DayOfWeek != DayOfWeek.Sunday)
                dateTime = dateTime.AddDays(1);
            return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);
        }

        public static DateTime GetLastDayOfYear(DateTime dateTime) {
            DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, 12, 31);
            return firstDayOfTheMonth;
        }

        public static string GetTpDateToday() {
            return DateTime.Today.Year.ToString() + "-" + DateTime.Today.Month.ToString("00") + "-" + DateTime.Today.Day.ToString("00");
        }

        public static bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle) {
            Double result;
            return Double.TryParse(val, NumberStyle,
                System.Globalization.CultureInfo.CurrentCulture, out result);
        }

        public static string Left(string text, int length) {
            if(length < 0)
                throw new ArgumentOutOfRangeException("length", length, "length must be > 0");
            else if(length == 0 || text.Length == 0)
                return "";
            else if(text.Length <= length)
                return text;
            else
                return text.Substring(0, length);
        }

        public static string Mid(string param, int startIndex, int length) {
            //start at the specified index in the string ang get N number of
            //characters depending on the lenght and assign it to a variable
            string result = param.Substring(startIndex, length);
            //return the result of the operation
            return result;
        }

        public static string Mid(string param, int startIndex) {
            //start at the specified index and return all characters after it
            //and assign it to a variable
            string result = param.Substring(startIndex);
            //return the result of the operation
            return result;
        }

        public static DateTime NextGoodBusinessDay(DateTime currentDay) {
            DateTime retDay = currentDay.Date.AddDays(1);
            if(retDay.DayOfWeek == DayOfWeek.Saturday) {
                retDay = currentDay.AddDays(3);
            }
            if(retDay.DayOfWeek == DayOfWeek.Sunday) {
                retDay = currentDay.AddDays(2);
            }
            foreach(DateTime ft in Feiertage) {
                if(retDay == ft) {
                    retDay = retDay.AddDays(1);
                }
            }
            foreach(DateTime ft in Feiertage) {
                if(retDay == ft) {
                    retDay = retDay.AddDays(1);
                }
            }
            return retDay;
        }

        public static T Nz<T>(T Value, T Replacement) {

            try {
                if(Value != null) {
                    if(Value.GetType() != Replacement.GetType()) {
                        if(Replacement.GetType().ToString().Contains("String")) {
                            object ret = Value.ToString();
                            return (T)ret;
                        }
                    }
                    return Value;
                } else {
                    return Replacement;
                }
            } catch(Exception e) {
                Debug.Print(e.ToString());
                return Replacement;
            }

        }

        public static string Right(string original, int numberCharacters) {
            return original.Substring(numberCharacters > original.Length ? 0 : original.Length - numberCharacters);
        }

        public static bool SheetsExists(Workbook wb, string sheetName) {
            foreach(Worksheet s in wb.Sheets) {
                if(s.Name == sheetName) {
                    return true;
                }
            }
            return false;
        }

        #endregion

    }
}
